/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __K_TIMER_H__
#define __K_TIMER_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_module.h>
/* includes (local) ----------------------------------------------------------*/
#include "k_errno.h"
#include "k_tick.h"
#include "k_obj.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define TIMER_STAT_INACTIVE   0 /* not in tick_q */
#define TIMER_STAT_ACTIVE     1 /* in tick_q */
#define TIMER_STATUS_DELETING 3

#define TIMER_FLAG_ONE_SHOT 0
#define TIMER_FLAG_REPEATED 1

#define ERRNO_TIMER_ILLEGAL_ID       ERRNO_JOIN(MID_TIMER, 1)
#define ERRNO_TIMER_ILLEGAL_FLAG     ERRNO_JOIN(MID_TIMER, 2)
#define ERRNO_TIMER_ILLEGAL_INTERVAL ERRNO_JOIN(MID_TIMER, 3)
#define ERRNO_TIMER_ILLEGAL_PFN      ERRNO_JOIN(MID_TIMER, 4)

/* typedefs ------------------------------------------------------------------*/

typedef struct timer {
    obj_t obj;
    uint16_t status;
    uint16_t flags;
    struct tick_q_node tq_node;
    unsigned long interval;
    void (*pfn)(uintptr_t);
    uintptr_t arg;
} timer_t, *timer_id;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern int timer_init(timer_id timer, uint16_t flags, unsigned long interval,
                      void (*pfn)(uintptr_t), uintptr_t arg);
extern timer_id timer_create(uint16_t flags, unsigned long interval, void (*pfn)(uintptr_t),
                             uintptr_t arg);
extern int timer_start(timer_id timer);
extern int timer_stop(timer_id timer);
extern int timer_delete(timer_id timer);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __K_TIMER_H__ */
